﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Security.Permissions;
using WebService.WSCardSystem;
using System.Net;
using System.Web.Services.Protocols;
namespace WebService
{
    /// <summary>
    /// Danh cho môi giới ngân hàng
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {



        [WebMethod]
        public string tenBank(string str)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            WSCardSystem.EMVServices proxy = new EMVServices();
            return proxy.GetBankCommercialNameOfCreditCard(str.Trim());
        }

        [WebMethod(Description = "Login cho môi giới ngân hàng", EnableSession = true)]
        public string Login(string UserName, string Password)
        {

            //String connectionString = "Data Source=PHUOCTOAN-PC\\SQLEXPRESS;Initial Catalog=NGANHANG;Integrated Security=True";
            String connectionString = "workstation id=DatabaseNganHang.mssql.somee.com;packet size=4096;user id=PTOAN;pwd=phuoctoan;data source=DatabaseNganHang.mssql.somee.com;persist security info=False;initial catalog=DatabaseNganHang";

            NganHangDataContext data = new NganHangDataContext(connectionString);
            //TaiKhoan taiKhoan = data.TaiKhoans.Where(c => c.TenDangNhap == UserName && c.PassWord == Password).FirstOrDefault();
            List<TaiKhoan> lst = data.TaiKhoans.Where(c => c.TenDangNhap == UserName && c.PassWord == Password).ToList();

            if (lst.Count() == 1)
            {
                Session["taikhoan"] = UserName;
                String sid = Session.SessionID;

                Session.Timeout = 300;
                return sid;
            }
            else
                return "0";

        }

        [WebMethod(Description = "Chuyển khoản nội ngân hàng và liên ngân hàng", EnableSession = true)]

        public int Tranfer(string sid, string strCardIDSend, string strCardIDReceive, int iAmount)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            WSCardSystem.EMVServices proxy = new EMVServices();
            proxy.CookieContainer = new CookieContainer();

            //String connectionString = "Data Source=PHUOCTOAN-PC\\SQLEXPRESS;Initial Catalog=NGANHANG;Integrated Security=True";
            String connectionString = "workstation id=DatabaseNganHang.mssql.somee.com;packet size=4096;user id=PTOAN;pwd=phuoctoan;data source=DatabaseNganHang.mssql.somee.com;persist security info=False;initial catalog=DatabaseNganHang";

            NganHangDataContext data = new NganHangDataContext(connectionString);
            The the = data.Thes.Where(c => c.MaThe == strCardIDSend).FirstOrDefault();
            string sID = proxy.Authenticate("NamABank", "b94DDu^xcC");
            int isCardIdSend = proxy.CardValid2(sID, strCardIDSend.Trim(), the.MaLoaiThe, the.MaSecure, the.TenChuThe);

            The the1 = data.Thes.Where(c => c.MaThe == strCardIDReceive).FirstOrDefault();
            List<NganHangLienMinh> nganHangLienMinh = data.NganHangLienMinhs.ToList();

            if (isCardIdSend != 0)
                return -2; // người gửi k thuộc quyền quản lý của khách hàng
            else if (double.Parse(the.SoDu.ToString()) < iAmount)
                return 0; // số tiền không đủ
            //else if (tenBank(strCardIDReceive) != "NAB")
            //    return -3; // số thẻ người nhận k thuộc ngân hàng liên kết
            else if (iAmount % 1000 != 0)
                return -5;// số tiền gửi chia hết cho 10k
            else if (sid != Session.SessionID)
                return -1;// chưa đăng nhập hoặc đã quá thời gian đăng nhập
            else if (tenBank(strCardIDReceive.Trim()) == "NAB")
            {
                the.SoDu = the.SoDu - iAmount;

                the1.SoDu = the1.SoDu + iAmount;
                data.SubmitChanges();
                ChiTietGD_The chiTiet = new ChiTietGD_The();
                chiTiet.LoaiGD_The = data.LoaiGD_Thes.Where(c => c.MaLoaiGD_The == 1).First();
                chiTiet.MaTheGui = strCardIDSend;
                chiTiet.MaTheNhan = strCardIDReceive;
                chiTiet.NgayGD = DateTime.Now;
                chiTiet.SoTien = iAmount;
                data.ChiTietGD_Thes.InsertOnSubmit(chiTiet);
                data.SubmitChanges();

                TheNhan theNhan = new TheNhan();
                theNhan.MaTheNhanOrGui = strCardIDReceive;
                theNhan.MaNganHangLienMinh = null;
                theNhan.MaChiTietGD_The = chiTiet.MaChiTietGD_The;
                data.TheNhans.InsertOnSubmit(theNhan);
                data.SubmitChanges();

                
               
                LichSuGiaoDichThe lichSu = new LichSuGiaoDichThe();
                lichSu.MaChiTietGD = chiTiet.MaChiTietGD_The;
                data.LichSuGiaoDichThes.InsertOnSubmit(lichSu);
                data.SubmitChanges();

                return iAmount;
            }
            else
            {
                for (int i = 0; i < nganHangLienMinh.Count; i++)

                    if (nganHangLienMinh[i].MaNHLienMinh == tenBank(strCardIDReceive.Trim()))
                    {
                        int temp = iAmount + Convert.ToInt32(iAmount * 0.05);
                        the.SoDu = iAmount;
                        data.SubmitChanges();
                        ChiTietGD_The chiTiet = new ChiTietGD_The();
                        chiTiet.LoaiGD_The = data.LoaiGD_Thes.Where(c => c.MaLoaiGD_The == 1).First();
                        chiTiet.MaTheGui = strCardIDSend;
                        chiTiet.MaTheNhan = strCardIDReceive;
                        chiTiet.NgayGD = DateTime.Now;
                        chiTiet.SoTien = temp;
                        data.ChiTietGD_Thes.InsertOnSubmit(chiTiet);
                        data.SubmitChanges();

                        TheNhan theNhan = new TheNhan();
                        theNhan.MaTheNhanOrGui = strCardIDReceive;
                        theNhan.MaNganHangLienMinh = nganHangLienMinh[i].MaNHLienMinh;
                        theNhan.MaChiTietGD_The = chiTiet.MaChiTietGD_The;
                        data.TheNhans.InsertOnSubmit(theNhan);

                        data.SubmitChanges();

                       

                        LichSuGiaoDichThe lichSu = new LichSuGiaoDichThe();
                        lichSu.MaChiTietGD = chiTiet.MaChiTietGD_The;
                        data.LichSuGiaoDichThes.InsertOnSubmit(lichSu);
                        data.SubmitChanges();
                        return temp;
                    }

            }

            return -3;
        }
        [WebMethod(Description = "Nhận tiền nội ngân hàng và liên ngân hàng", EnableSession = true)]
        public int Withdraw(string sid, string strCardIDReceive, string strCardIDSend, int iAmount)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            WSCardSystem.EMVServices proxy = new EMVServices();
            proxy.CookieContainer = new CookieContainer();
            //String connectionString = "Data Source=PHUOCTOAN-PC\\SQLEXPRESS;Initial Catalog=NGANHANG;Integrated Security=True";
             string connectionString = "workstation id=DatabaseNganHang.mssql.somee.com;packet size=4096;user id=PTOAN;pwd=phuoctoan;data source=DatabaseNganHang.mssql.somee.com;persist security info=False;initial catalog=DatabaseNganHang";

            NganHangDataContext data = new NganHangDataContext(connectionString);
            The theNguoiNhan = data.Thes.Where(c => c.MaThe == strCardIDReceive).FirstOrDefault();
            string tenBank = proxy.GetBankCommercialNameOfCreditCard(strCardIDSend.Trim());
            // The theNguoiGui = data.Thes.Where(c => c.MaThe == strCardIDSend).FirstOrDefault();
            List<NganHangLienMinh> nganHangLienMinh = data.NganHangLienMinhs.ToList();
            if (sid != Session.SessionID)
                return -1;// chua đăng nhập hoặc quá thời gian đăng nhập

            else if (iAmount % 1000 != 0)
                return -5; // số tiền nhận phải chia hết 10k
            else
                for (int i = 0; i < nganHangLienMinh.Count; i++)
                {
                    if (nganHangLienMinh[i].MaNHLienMinh == tenBank)
                    {
                        int temp = iAmount + Convert.ToInt32(iAmount * 0.05);
                        theNguoiNhan.SoDu = iAmount;
                        data.SubmitChanges();
                        ChiTietGD_The chiTiet = new ChiTietGD_The();
                        chiTiet.LoaiGD_The = data.LoaiGD_Thes.Where(c => c.MaLoaiGD_The == 2).First();
                        chiTiet.MaTheGui = strCardIDReceive;
                        chiTiet.MaTheNhan = strCardIDSend;
                        chiTiet.NgayGD = DateTime.Now;
                        chiTiet.SoTien = temp;
                        data.ChiTietGD_Thes.InsertOnSubmit(chiTiet);
                        data.SubmitChanges();

                        TheNhan theNhan = new TheNhan();
                        theNhan.MaTheNhanOrGui = strCardIDSend;
                        theNhan.MaNganHangLienMinh = nganHangLienMinh[i].MaNHLienMinh;
                        theNhan.MaChiTietGD_The = chiTiet.MaChiTietGD_The;
                        data.TheNhans.InsertOnSubmit(theNhan);
                        data.SubmitChanges();

                       

                        LichSuGiaoDichThe lichSu = new LichSuGiaoDichThe();
                        lichSu.MaChiTietGD = chiTiet.MaChiTietGD_The;
                        data.LichSuGiaoDichThes.InsertOnSubmit(lichSu);
                        data.SubmitChanges();
                        return temp;
                    }

                }

            return -3;
        }

    }
}